#include "myTask.h"
#include <stdio.h>
#include <string.h>
#include "cmdProcess.h"
#include "redstone_repeater.h"

void task_heartbeat_func(void *param);
void task_cmd_func(void *param);
void task_adc_func(void *param);
void task_redstone_func(void *param);
void task_repeater_func(void *param);

volatile uint32_t realTicks; // 系统时间，溢出处理 todo
uint8_t flag_schedule = 0; // 调度开关标志位

// 任务控制块数组
taskCtrl taskList[] = {
    {
        .run = 1, // 立即执行
        .tickCounts = 100,
        .tickReload = 100,
        .flag = NULL,
        .pTaskFunc = task_heartbeat_func,
    },

    {
        .run = 0,
        .tickCounts = 200,
        .tickReload = 200,
        .flag = NULL,
        .pTaskFunc = task_cmd_func,
    },

    {
        .run = 0,
        .tickCounts = 5,
        .tickReload = 5,
        .flag = &flag_conversion_complete,
        .pTaskFunc = task_adc_func,
    },

    {
        .run = 0,
        .tickCounts = RS_GAME_TICK,
        .tickReload = RS_GAME_TICK,
        .flag = NULL,
        .pTaskFunc = task_redstone_func,
    },

    {
        .run = 0,
        .tickCounts = 0, // 挂起
        .tickReload = RS_GAME_TICK*2,
        .flag = NULL,
        .pTaskFunc = task_repeater_func,
    },

    // end of list
    {
        .tickReload = 0,
    },
};


// 心拍任务
void task_heartbeat_func(void *param){

    // static uint8_t i = 0;

    // LOG_FMT("Heartbeat: %d\n", i ++);
}

// 处理命令任务
void task_cmd_func(void *param){
    static uint8_t cmd_buffer[CMDBUF_SIZE];

    // 读取命令
    if(SEGGER_RTT_HasData(0)){
        int len = SEGGER_RTT_Read(0, cmd_buffer, sizeof(cmd_buffer) - 1);
        if (len > 0) {
            cmd_buffer[len] = '\0'; // 添加字符串终止符

            process_command((char *)cmd_buffer); // 处理命令
        }
    }
}

uint16_t adc_redstone_back;
uint16_t adc_redstone_left;
uint16_t adc_redstone_right;
uint16_t adc_vref; // 内部参考电压
uint16_t adc_temp; // 内部温度传感器
uint16_t real_Vref = 1500; // 实际内部参考电压，毫伏
// ADC 读取任务
void task_adc_func(void *param){
    uint8_t *flag = ((taskCtrl *)param)->flag;

    // 处理 ADC 转换完成中断
    if(*flag){
        *flag = 0;

        adc_redstone_left = ADC_converted_data[0];
        adc_redstone_back = ADC_converted_data[1];
        adc_redstone_right = ADC_converted_data[2];
        adc_temp = ADC_converted_data[3]; // 内部温度传感器
        adc_vref = ADC_converted_data[4]; // 内部参考电压
        real_Vref = 4095*1200/adc_vref;
    }else {
        HAL_ADC_Start_IT(&hadc);
    }
}

// 写得乱七八糟的，得重构 myTask
typedef enum{
    RP_CMD_NONE,
    RP_CMD_ON,
    RP_CMD_OFF,
} rp_cmd_e;
uint32_t *rp_tick_counts = &(taskList[4].tickCounts);
uint8_t rp_cmd = RP_CMD_NONE;
// 处理红石输入
void task_redstone_func(void *param){
    static uint8_t last_latch_state = 0;
    static uint8_t last_rs_state = 0;

    
    if((ReAL_get_strength_min(&(myRS_repeater.rs_left)) > RS_STRENGTH_15) || (ReAL_get_strength_min(&(myRS_repeater.rs_right)) > RS_STRENGTH_15)){
        // 有任意一个锁存信号输入
        last_latch_state = 1;
        myRS_repeater.write_pin(&myRS_repeater, RP_PIN_LATCH_LED, RP_LED_ON);
        // 锁存状态，不变化输出
        *rp_tick_counts = 0;
        rp_cmd = RP_CMD_NONE;

        return ;

    }else { // 无锁存信号
        myRS_repeater.write_pin(&myRS_repeater, RP_PIN_LATCH_LED, RP_LED_OFF);
        if(last_latch_state == 1){ // 从有锁存变为无锁存，将在指定挡位延时后改变信号
            if(ReAL_get_strength_min(&(myRS_repeater.rs_back)) > RS_STRENGTH_0){
                last_rs_state = 1;
            }else {
                last_rs_state = 0;
            }
            *rp_tick_counts = 2*RS_GAME_TICK*RS_repeater_get_gear(&myRS_repeater);
            if(last_rs_state){
                rp_cmd = RP_CMD_ON;
            }else {
                rp_cmd = RP_CMD_OFF;
            }
        }else { // 一直是无锁存
            if(ReAL_get_strength_min(&(myRS_repeater.rs_back)) > RS_STRENGTH_0){
                if(!last_rs_state){ // 后方由无信号变为有信号
                    *rp_tick_counts = 2*RS_GAME_TICK*RS_repeater_get_gear(&myRS_repeater);
                    rp_cmd = RP_CMD_ON;

                }
                last_rs_state = 1;
            }else {
                if(last_rs_state){ // 后方由有信号变为无信号
                    *rp_tick_counts = 2*RS_GAME_TICK*RS_repeater_get_gear(&myRS_repeater);
                    rp_cmd = RP_CMD_OFF;

                }
                last_rs_state = 0;

            }
        }
        last_latch_state = 0;
    }

}

void task_repeater_func(void *param){
    switch(rp_cmd){
        case RP_CMD_NONE:

            task_self_pending((taskCtrl *)param); // tickCounts 溢出处理
            return ;

            break;

        case RP_CMD_ON:
            myRS_repeater.write_pin(&myRS_repeater, RP_PIN_OUTPUT_LED, RP_LED_ON);
            myRS_repeater.write_pin(&myRS_repeater, RP_PIN_OUTPUT, RP_OUT_ON);
            task_self_pending((taskCtrl *)param);

            break;

        case RP_CMD_OFF:
            myRS_repeater.write_pin(&myRS_repeater, RP_PIN_OUTPUT_LED, RP_LED_OFF);
            myRS_repeater.write_pin(&myRS_repeater, RP_PIN_OUTPUT, RP_OUT_OFF);
            task_self_pending((taskCtrl *)param);

            break;

        default:
            task_self_pending((taskCtrl *)param);
            break;
    }
}


void task_schedule(void){
    uint8_t i;
    
    // 当倒计时结束或设定的任务标志位非 0，运行该任务
    for(i = 0; taskList[i].tickReload != 0; i++){
        if(taskList[i].flag != NULL && *(taskList[i].flag) != 0){
            taskList[i].run ++;
        }
        if(taskList[i].run != 0){
            taskList[i].run --;
            taskList[i].pTaskFunc(&taskList[i]);
        }
    }
}

void tick_tack(void){
    if(!flag_schedule){
        return ;
    }

    uint8_t i;
    realTicks ++;
    for(i = 0; taskList[i].tickReload != 0; i++){
        if(--taskList[i].tickCounts == 0){
            taskList[i].tickCounts = taskList[i].tickReload;
            taskList[i].run ++;
        }
    }
}

void start_schedule(void){
    flag_schedule = 1;
}

void stop_schedule(void){
    flag_schedule = 0;
}

void task_self_pending(taskCtrl *task){
    task->tickCounts = 0;
}
